var typed = 0;
var floorIdx = 0;
var C = "";
var itemList = Array(4000019, 4000000, 4000016);

function start() {
    status = -1;
    action(1, 0, 0)
}

function action(m, d, v) {
    var c = cm.getEventManager("Mzhy");
    var g = c.getInstance("Mzhy");
    if (status == 0 && m == 0) {
        cm.dispose();
        return
    }
    if (m == 1) {
        status++
    } else {
        status--
    }
    if (status == 0) {
        if (c.getProperty("state") == 1) {
            c.setProperty("state", 2);
            cm.dispose();
            cm.openNpc(9070010, 2);
            return
        }
        var l = "";
        if (cm.getMap(931050410).getNumMonsters() == 0) {
            floorIdx = parseInt(g.getProperty("currentFloor")) - 1;
            if (floorIdx == 19) {
                cm.setBossLog("迷之幻域");
                var k = g.getTimeLeft();
                var f = 60 * 1000 * 30;
                var t = f - k;
                var n = Math.floor(t / (60 * 1000));
                var q = Math.floor(t % (60 * 1000) / 1000);
                cm.warp(910000000);
                var p = Array(Array(4001839, 3), Array(5062002, 2), Array(5062500, 2), Array(5064000, 1), Array(2430069, 2), Array(4002001, 1), Array(5150040, 3), Array(2430781, 3), Array(2340000, 1));
                l = "共使用了" + n + "分" + q + "秒通关。任务奖励：\r\n";
                for (var u in p) {
                    var i = p[u][0];
                    var h = p[u][1];
                    l += "#b#v" + i + "##t" + i + "# #rx" + h + "#k\r\n";
                    cm.gainItem(i, h)
                }
                cm.worldSpouseMessage(23, "[迷之幻域] : 【" + cm.getPlayer().getName() + "】花费了<" + n + "分" + q + "秒>成功通关，获得大量奖励！ ");
                cm.sendOk(l);
                cm.dispose()
            } else {
                l += "我是第#r#e" + (floorIdx + 1) + "#n#k层的#b时空门#k，请选择开启一个机关尝试消除幻境：\r\n";
                l += "#d#L0##v4000019#开启机关[剩余" + cm.getItemQuantity(4000019) + "个]\r\n";
                l += "#b#L1##v4000000#开启机关[剩余" + cm.getItemQuantity(4000000) + "个]\r\n";
                l += "#r#L2##v4000016#开启机关[剩余" + cm.getItemQuantity(4000016) + "个]\r\n";
                l += "\r\n";
                l += "#L3##d#e放弃挑战#n#k#l";
                cm.askMenu(l)
            }
        } else {
            cm.sendOk("我的勇士，击败黑魔法师的门徒才能开启机关！战胜他们吧！");
            cm.dispose()
        }
    } else {
        if (status == 1) {
            var o = v;
            if (o > 2) {
                if (o == 4) {
                    C = "购买蜗牛壳";
                    cm.askMenu("你想购买什么颜色的蜗牛壳呢？100万可以买20个~\r\n#b#L0#购买#v4000019#绿色蜗牛壳#l\r\n#L1#购买#v4000000#蓝色蜗牛壳#l\r\n#L2#购买#v4000016#红色蜗牛壳#l")
                } else {
                    if (o == 3) {
                        C = "放弃挑战";
                        cm.askYesNo("你真的要强行离开迷之幻域吗？那样的话什么奖励都没有。")
                    }
                }
            } else {
                C = "开启机关";
                if (!cm.haveItem(itemList[o])) {
                    cm.sendOk("你的#v" + itemList[o] + "#数量不够，无法开启此机关。");
                    cm.dispose();
                    return
                }
                cm.gainItem(itemList[o], -1);
                var a = Array();
                var s = g.getProperty("floor" + floorIdx);
                a = s.split(",");
                var j = 1 * a[o];
                if (cm.getPlayer().getName() == "管理员心动蓝蜗牛") {
                    j = 25
                }
                if ((floorIdx + j) <= 0) {
                    j = 0
                }
                if ((floorIdx + j) >= 20) {
                    j = 19 - floorIdx
                }
                var b = "";
                var e = floorIdx + 1 + j;
                if (j > 0) {
                    b = "嗯，运气真好，来到了第" + (e) + "层幻境。"
                } else {
                    if (j == 0) {
                        b = "唔~还是在第" + (e) + "层幻境啊。"
                    } else {
                        b = "悲剧了！回到了第" + (e) + "层幻境。"
                    }
                }
                g.setProperty("currentFloor", e);
                var r = g.getMapInstance(0);
                cm.getPlayer().changeMap(r, r.getPortal(2));
                cm.getPlayer().dropMessage(1, b);
                cm.dispose()
            }
        } else {
            if (status == 2) {
                if (C == "放弃挑战") {
                    cm.getPlayer().dropMessage(1, "终于回到了现实世界……");
                    cm.warp(910000000);
                    cm.dispose()
                } else {
                    if (C == "购买蜗牛壳") {
                        if (cm.getMeso() < 1000000) {
                            cm.sendOk("钱不够噢~");
                            cm.dispose();
                            return
                        }
                        cm.sendOk("购买成功！");
                        cm.gainItem(itemList[v], 20);
                        cm.gainMeso(-1000000);
                        cm.dispose()
                    }
                }
            }
        }
    }
}
var status = -1;